<?php

namespace app\index\controller;

use think\Db;
use think\Cookie;

class Index extends Base {

	/**
	 * 首页 行情列表
	 * @author lukui  2017-02-18
	 * @return [type] [description]
	 */
	public function index() {
		//获取产品信息
		$pro = Db::name('productinfo')->alias('pi')->field('pi.pid,pi.ptitle,pd.Price,pd.UpdateTime,pd.Low,pd.High')
				->join('__PRODUCTDATA__ pd', 'pd.pid=pi.pid')
				->where('pi.isdelete', 0)->order('pi.proorder asc')->select();
		//dump(cookie('pid7'));
		$this->assign('pro', $pro);
		return $this->fetch();
	}

	public function ajaxindexpro() {

		//获取产品信息
		$pro = Db::name('productinfo')->alias('pi')->field('pi.pid,pi.ptitle,pd.Price,pd.UpdateTime,pd.Low,pd.High')
				->join('__PRODUCTDATA__ pd', 'pd.pid=pi.pid')
				->where('pi.isdelete', 0)->order('pi.pid desc')->select();
		$newpro = array();
		foreach ($pro as $k => $v) {
			$newpro[$v['pid']] = $pro[$k];
			$newpro[$v['pid']]['UpdateTime'] = date('H:i:s', $v['UpdateTime']);
			// if(!isset($_COOKIE['pid'.$v['pid']])){
			//     cookie('pid'.$v['pid'],$v['Price']);
			//     continue;
			// }
			if ($v['Price'] < cookie('pid' . $v['pid'])) {  //跌了
				$newpro[$v['pid']]['isup'] = 0;
			} elseif ($v['Price'] > cookie('pid' . $v['pid'])) {  //涨了
				$newpro[$v['pid']]['isup'] = 1;
			} else {  //没跌没涨
				$newpro[$v['pid']]['isup'] = 2;
			}

			cookie('pid' . $v['pid'], $v['Price']);
		}

		return $newpro;
	}

}
